#!/bin/bash

# VEPAI平台部署脚本 (Linux版本 - 从backend目录运行)
# 使用方法: ./deploy.sh [start|stop|restart|status|logs|images]

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

ACTION=${1:-start}

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}  VEPAI平台部署脚本${NC}"
echo -e "${BLUE}  操作: ${ACTION}${NC}"
echo -e "${BLUE}  运行目录: $(pwd)${NC}"
echo -e "${BLUE}========================================${NC}"

# 检查当前目录是否正确
if [[ ! -f "run_prod.py" ]] && [[ ! -f "run.py" ]]; then
    echo -e "${RED}错误: 请在backend目录中运行此脚本${NC}"
    exit 1
fi

# 检查Docker是否安装
if ! command -v docker &> /dev/null; then
    echo -e "${RED}错误: Docker未安装或未启动${NC}"
    exit 1
fi

# 检查docker-compose是否安装
if ! command -v docker-compose &> /dev/null && ! command -v docker compose &> /dev/null; then
    echo -e "${RED}错误: docker-compose未安装${NC}"
    exit 1
fi

# 使用正确的docker-compose命令
DOCKER_COMPOSE="docker-compose"
if command -v docker compose &> /dev/null; then
    DOCKER_COMPOSE="docker compose"
fi

case $ACTION in
    start)
        echo -e "${YELLOW}启动VEPAI平台...${NC}"
        
        # 创建必要的目录
        mkdir -p ../logs ../data
        
        # 启动服务
        $DOCKER_COMPOSE -f docker-compose.prod.yml up -d
        
        echo -e "${GREEN}VEPAI平台启动成功！${NC}"
        echo -e "${BLUE}前端访问地址: http://192.168.253.41${NC}"
        echo -e "${BLUE}后端API地址: http://192.168.253.41:5000${NC}"
        
        # 等待服务启动
        echo -e "${YELLOW}等待服务启动...${NC}"
        sleep 10
        
        # 检查服务状态
        $DOCKER_COMPOSE -f docker-compose.prod.yml ps
        ;;
        
    stop)
        echo -e "${YELLOW}停止VEPAI平台...${NC}"
        $DOCKER_COMPOSE -f docker-compose.prod.yml down
        echo -e "${GREEN}VEPAI平台已停止${NC}"
        ;;
        
    restart)
        echo -e "${YELLOW}重启VEPAI平台...${NC}"
        $DOCKER_COMPOSE -f docker-compose.prod.yml down
        sleep 3
        $DOCKER_COMPOSE -f docker-compose.prod.yml up -d
        
        echo -e "${GREEN}VEPAI平台重启成功！${NC}"
        echo -e "${BLUE}前端访问地址: http://192.168.253.41${NC}"
        echo -e "${BLUE}后端API地址: http://192.168.253.41:5000${NC}"
        
        # 等待服务启动
        echo -e "${YELLOW}等待服务启动...${NC}"
        sleep 10
        
        # 检查服务状态
        $DOCKER_COMPOSE -f docker-compose.prod.yml ps
        ;;
        
    status)
        echo -e "${YELLOW}检查VEPAI平台状态...${NC}"
        $DOCKER_COMPOSE -f docker-compose.prod.yml ps
        
        # 检查端口占用
        echo -e "\n${BLUE}端口占用情况:${NC}"
        netstat -tlnp | grep -E ':80|:5000' || echo "端口未被占用"
        ;;
        
    logs)
        echo -e "${YELLOW}查看VEPAI平台日志...${NC}"
        $DOCKER_COMPOSE -f docker-compose.prod.yml logs -f
        ;;
        
    update)
        echo -e "${YELLOW}更新VEPAI平台...${NC}"
        
        # 拉取最新镜像
        $DOCKER_COMPOSE -f docker-compose.prod.yml pull
        
        # 重启服务
        $DOCKER_COMPOSE -f docker-compose.prod.yml down
        $DOCKER_COMPOSE -f docker-compose.prod.yml up -d
        
        echo -e "${GREEN}VEPAI平台更新完成！${NC}"
        ;;
        
    clean)
        echo -e "${YELLOW}清理VEPAI平台...${NC}"
        
        # 停止并删除容器
        $DOCKER_COMPOSE -f docker-compose.prod.yml down -v
        
        # 删除镜像
        docker rmi vepai-backend:latest vepai-frontend:latest 2>/dev/null || true
        
        # 清理无用镜像
        docker system prune -f
        
        echo -e "${GREEN}VEPAI平台清理完成！${NC}"
        ;;
        
    images)
        echo -e "${YELLOW}查看Docker镜像...${NC}"
        echo -e "\n${BLUE}所有VEPAI相关镜像:${NC}"
        docker images | grep vepai || echo "未找到VEPAI镜像"
        echo -e "\n${BLUE}所有Docker镜像:${NC}"
        docker images
        ;;
        
    *)
        echo -e "${RED}未知操作: $ACTION${NC}"
        echo -e "${YELLOW}可用操作:${NC}"
        echo -e "  start   - 启动平台"
        echo -e "  stop    - 停止平台"
        echo -e "  restart - 重启平台"
        echo -e "  status  - 检查状态"
        echo -e "  logs    - 查看日志"
        echo -e "  update  - 更新平台"
        echo -e "  clean   - 清理平台"
        echo -e "  images  - 查看镜像"
        exit 1
        ;;
esac

echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}  操作完成！${NC}"
echo -e "${GREEN}========================================${NC}" 